﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using SmartXLS;
using System.Drawing;
using UygulamaTakbisService.wsTakbisServis;

namespace UygulamaTakbisService
{
    public class SbiYaz
    {
        public byte[] Yaz(List<SBI> sbiList, string serverKayitYolu)
        {
            WorkBook workBook = new WorkBook();
            workBook.setSheetName(0, "Sbi Listesi");

            int satir = 0;
            int sutun = 0;
            workBook.setText(satir, sutun, "SBITip");
            sutun++;
            workBook.setText(satir, sutun, "Şablon");
            sutun++;
            workBook.setText(satir, sutun, "Aktif Mi");
            sutun++;
            workBook.setText(satir, sutun, "Metin");
            sutun++;
            workBook.setText(satir, sutun, "Başlama Tarih");
            sutun++;
            workBook.setText(satir, sutun, "Bitiş Tarih");
            sutun++;
            workBook.setText(satir, sutun, "Süre");
            sutun++;
            workBook.setText(satir, sutun, "Birim");
            sutun++;
            workBook.setText(satir, sutun, "Tesis İşlem Tanım Adı");
            sutun++;
            workBook.setText(satir, sutun, "Tesis İşlem Yevmiye No");
            sutun++;
            workBook.setText(satir, sutun, "Tesis İşlem Yevmiye Tarih");
            sutun++;
            workBook.setText(satir, sutun, "Tesis İşlem Yevmiye Kurum");
            sutun++;
            workBook.setText(satir, sutun, "Terkin İşlem Tanım Adı");
            sutun++;
            workBook.setText(satir, sutun, "Terkin İşlem Yevmiye No");
            sutun++;
            workBook.setText(satir, sutun, "Terkin İşlem Yevmiye Tarih");
            sutun++;
            workBook.setText(satir, sutun, "Terkin İşlem Yevmiye Kurum");
            sutun++;
            workBook.setText(satir, sutun, "Tbsb AktifMi");
            sutun++;
            workBook.setText(satir, sutun, "Tbsb Tesis İşlem Tanım Adı");
            sutun++;
            workBook.setText(satir, sutun, "Tbsb Tesis İşlem Yevmiye No");
            sutun++;
            workBook.setText(satir, sutun, "Tbsb Tesis İşlem Yevmiye Tarih");
            sutun++;
            workBook.setText(satir, sutun, "Tbsb Tesis İşlem Yevmiye Kurum");
            sutun++;
            workBook.setText(satir, sutun, "Tbsb Terkin İşlem Tanım Adı");
            sutun++;
            workBook.setText(satir, sutun, "Tbsb Terkin İşlem Yevmiye No");
            sutun++;
            workBook.setText(satir, sutun, "Tbsb Terkin İşlem Yevmiye Tarih");
            sutun++;
            workBook.setText(satir, sutun, "Tbsb Terkin İşlem Yevmiye Kurum");
            sutun++;

            RangeStyle rangeStyleBaslik = workBook.getRangeStyle(0, 0, 0, sutun);
            rangeStyleBaslik.FontBold = true;
            rangeStyleBaslik.FontColor = Color.DarkRed.ToArgb();
            rangeStyleBaslik.HorizontalAlignment = RangeStyle.HorizontalAlignmentCenter;
            rangeStyleBaslik.VerticalAlignment = RangeStyle.VerticalAlignmentCenter;
            rangeStyleBaslik.LeftBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.RightBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.TopBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.BottomBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.VerticalInsideBorder = RangeStyle.BorderThin;
            workBook.setRangeStyle(rangeStyleBaslik, 0, 0, 0, sutun - 1);

            string deger = string.Empty;
            foreach (SBI sbi in sbiList)
            {
                satir++;
                sutun = 0;
                if (sbi.SBITip != null)
                    deger = sbi.SBITip.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.SBISablon != null)
                {
                    if (sbi.SBISablon.Ad != null)
                        deger = sbi.SBISablon.Ad.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TapuBolumDurum != null)
                    deger = sbi.TapuBolumDurum.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.SBICumle != null)
                    deger = sbi.SBICumle.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.BaslamaTarih != null)
                    deger = sbi.BaslamaTarih.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.BitisTarih != null)
                    deger = sbi.BitisTarih.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.Sure != null)
                    deger = sbi.Sure.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.SureBirim != null)
                {
                    if (sbi.SureBirim.Ad != null)
                        deger = sbi.SureBirim.Ad.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TesisIslem != null)
                {
                    if (sbi.TesisIslem.IslemTanimAd != null)
                        deger = sbi.TesisIslem.IslemTanimAd.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TesisIslem != null && sbi.TesisIslem.Yevmiye != null)
                {
                    if (sbi.TesisIslem.Yevmiye.YevmiyeNo != null)
                        deger = sbi.TesisIslem.Yevmiye.YevmiyeNo.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TesisIslem != null && sbi.TesisIslem.Yevmiye != null)
                {
                    if (sbi.TesisIslem.Yevmiye.Tarih != null)
                        deger = sbi.TesisIslem.Yevmiye.Tarih.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TesisIslem != null && sbi.TesisIslem.Yevmiye != null)
                {
                    if (sbi.TesisIslem.Yevmiye.Kurum != null)
                        deger = sbi.TesisIslem.Yevmiye.Kurum.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TerkinIslem != null)
                {
                    if (sbi.TerkinIslem.IslemTanimAd != null)
                        deger = sbi.TerkinIslem.IslemTanimAd.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TerkinIslem != null && sbi.TerkinIslem.Yevmiye != null)
                {
                    if (sbi.TerkinIslem.Yevmiye.Tarih != null)
                        deger = sbi.TerkinIslem.Yevmiye.YevmiyeNo.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TerkinIslem != null && sbi.TerkinIslem.Yevmiye != null)
                {
                    if (sbi.TerkinIslem.Yevmiye.Tarih != null)
                        deger = sbi.TerkinIslem.Yevmiye.Tarih.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TerkinIslem != null && sbi.TerkinIslem.Yevmiye != null)
                {
                    if (sbi.TerkinIslem.Yevmiye.Kurum != null)
                        deger = sbi.TerkinIslem.Yevmiye.Kurum.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TbsbAktifMi != null)
                    deger = sbi.TbsbAktifMi.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TbsbTesisIslem != null)
                {
                    if (sbi.TbsbTesisIslem.IslemTanimAd != null)
                        deger = sbi.TbsbTesisIslem.IslemTanimAd.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TbsbTesisIslem != null && sbi.TbsbTesisIslem.Yevmiye != null)
                {
                    if (sbi.TbsbTesisIslem.Yevmiye.YevmiyeNo != null)
                        deger = sbi.TbsbTesisIslem.Yevmiye.YevmiyeNo.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TbsbTesisIslem != null && sbi.TbsbTesisIslem.Yevmiye != null)
                {
                    if (sbi.TbsbTesisIslem.Yevmiye.Tarih != null)
                        deger = sbi.TbsbTesisIslem.Yevmiye.Tarih.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TbsbTesisIslem != null && sbi.TbsbTesisIslem.Yevmiye != null)
                {
                    if (sbi.TbsbTesisIslem.Yevmiye.Kurum != null)
                        deger = sbi.TbsbTesisIslem.Yevmiye.Kurum.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TbsbTerkinIslem != null)
                {
                    if (sbi.TbsbTerkinIslem.IslemTanimAd != null)
                        deger = sbi.TbsbTerkinIslem.IslemTanimAd.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TbsbTerkinIslem != null && sbi.TbsbTerkinIslem.Yevmiye != null)
                {
                    if (sbi.TbsbTerkinIslem.Yevmiye.YevmiyeNo != null)
                        deger = sbi.TbsbTerkinIslem.Yevmiye.YevmiyeNo.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TbsbTerkinIslem != null && sbi.TbsbTerkinIslem.Yevmiye != null)
                {
                    if (sbi.TbsbTerkinIslem.Yevmiye.Tarih != null)
                        deger = sbi.TbsbTerkinIslem.Yevmiye.Tarih.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (sbi.TbsbTerkinIslem != null && sbi.TbsbTerkinIslem.Yevmiye != null)
                {
                    if (sbi.TbsbTerkinIslem.Yevmiye.Kurum != null)
                        deger = sbi.TbsbTerkinIslem.Yevmiye.Kurum.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                RangeStyle rangeStyleVeri = workBook.getRangeStyle(satir, 0, satir, sutun);
                rangeStyleVeri.HorizontalAlignment = RangeStyle.HorizontalAlignmentLeft;
                rangeStyleVeri.VerticalAlignment = RangeStyle.VerticalAlignmentCenter;
                rangeStyleVeri.LeftBorder = RangeStyle.BorderThin;
                rangeStyleVeri.RightBorder = RangeStyle.BorderThin;
                rangeStyleVeri.TopBorder = RangeStyle.BorderThin;
                rangeStyleVeri.BottomBorder = RangeStyle.BorderThin;
                rangeStyleVeri.VerticalInsideBorder = RangeStyle.BorderThin;
                workBook.setRangeStyle(rangeStyleVeri, satir, 0, satir, sutun - 1);
            }

            for (int i = 0; i < sutun + 1; i++)
                workBook.setColWidthAutoSize(i, true);

            workBook.write(serverKayitYolu);
            workBook.Dispose();

            GC.GetTotalMemory(false);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.GetTotalMemory(true);

            return File.ReadAllBytes(serverKayitYolu);
        }
    }
}

